<template>
    <div>
        <h3>病人用药显示</h3>
        医生名称
        <input type="text" v-model="brr.Name" />
        科室
        <select v-model="brr.KeshiName">
            <option value="">请选择</option>
            <option v-for="item in arr" :value="item.keShiName">{{ item.keShiName }}</option>
        </select>
        <input type="button" value="查询" @click="YongYaoShow" class="btn btn-info" />
        <input type="button" value="批量完成" @click="Update(true)" class="btn btn-danger" />
        <table class="table table-bordered">
            <tbody>
                <tr>
                    <td><input type="checkbox" @click="UpAll()" v-model="check" /></td>
                    <td>药品名称</td>
                    <td>科室</td>
                    <td>用药日期</td>
                    <td>病人名称</td>
                    <td>病房号码</td>
                    <td>用药状态</td>
                    <td>配药医生</td>
                    <td>操作</td>
                </tr>
                <tr v-for="item in frr.list" :key="item.yongYaoInfoId">
                    <td><input type="checkbox" @change="down()" v-model="ids" :value="item.yongYaoInfoId" /></td>
                    <td>{{ item.yongYaoInfoName }}</td>
                    <td>{{ item.keShiName }}</td>
                    <td>{{ item.yongYaoInfoTime.substring(0, 10) }}</td>
                    <td>{{ item.bingRenName }}</td>
                    <td>{{ item.bingRenNum }}</td>
                    <td>{{ item.bingRenState == true ? '已完成' : '未开始' }}</td>
                    <td>{{ item.doctor }}</td>
                    <td>
                        <input type="button" value="删除" @click="Del(item.yongYaoInfoId)" class="btn btn-danger" />
                        <input type="button" value="修改" @click="FanTian(item.yongYaoInfoId)" class="btn btn-info" />

                    </td>

                </tr>
            </tbody>
        </table>
        <div>
            共<span style="color: red;">{{ frr.totalCount }}</span>数，每页<span style="color: red;">{{ brr.PageSize
                }}</span>条，共<span style="color: red;">{{ brr.PageIndex
                }}</span>/<span style="color: red;">{{ frr.totalPage }}</span>页
            <input type="button" value="首页" class="btn btn-info" @click="Page(1)" />
            <input type="button" value="上一页" class="btn btn-info" @click="Page(brr.PageIndex - 1)" />
            <input type="button" value="下一页" class="btn btn-info" @click="Page(brr.PageIndex + 1)" />
            <input type="button" value="尾页" class="btn btn-info" @click="Page(frr.totalPage)" />
        </div>
    </div>
</template>

<script setup lang="ts">
import { reactive, ref, onMounted } from 'vue';
import axios from 'axios';
import { useRoute, useRouter } from 'vue-router';

let arr: any = ref([])
onMounted(() => {
    Show();
    YongYaoShow();
})

let brr: any = ref({
    PageIndex: 1,
    PageSize: 2,
    Name: "",
    KeshiName: "",
})

let frr: any = ref({
    "totalPage": '',
    "totalCount": '',
    "list": []
})
//下拉框
const Show = () => {


    axios({
        url: "/api/KeShi/KeShiShow",
        method: "get",
    })
        .then(res => {
            arr.value = res.data;
        })
        .catch(err => {

        })


}

//显示
const YongYaoShow = () => {


    axios({
        url: "/api/KeShi/YongYaoInfoShow",
        method: "get",
        params: brr.value
    })
        .then(res => {
            frr.value.totalPage = res.data.totalPage;
            frr.value.totalCount = res.data.totalCount;
            frr.value.list = res.data.list;
        })
        .catch(err => {

        })


}

//翻页
const Page = (num: any) => {
    if (num < 1) {
        num = 1;
    }
    if (num > frr.value.totalPage) {
        num = frr.value.totalPage;
    }
    brr.value.PageIndex = num;
    YongYaoShow();
}


//全选
let check = ref(false);
let ids: any = ref({});

const UpAll = () => {
    check.value = !check.value;
    if (check.value == true) {
        ids.value = frr.value.list.map((item: any) => {
            return item.yongYaoInfoId;
        })
    } else {
        ids.value = [];
    }
}
const down = () => {
    if (ids.value.length == frr.value.list.length) {
        check.value = true
    }
    else {
        check.value = false;
    }
}


//批量
const Update = (state: any) => {
    axios({
        url: "/api/KeShi/AllUp",
        method: "put",
        params: {
            ids: ids.value.toString(),
            state: state

        }

    })


        .then(res => {
            if (res.data > 0) {
                alert('修改成功');
                check.value = false;
                ids.value = [];
                YongYaoShow();
            }
            else {
                alert('修改失败');
            }
        })
        .catch(err => {
            console.log(err.data);

        })

}


//删除

const Del = (id: any) => {
    let a = confirm('确认删除么?')
    if (a) {
        axios({
            url: "/api/KeShi/YongYaoDel",
            method: "delete",
            params: {
                id: id

            }

        })


            .then(res => {
                if (res.data > 0) {
                    alert('删除成功');
                    YongYaoShow();
                }
                else {
                    alert('删除失败');
                }
            })
            .catch(err => {
                console.log(err.data);

            })

    }

}


let router = useRouter();

const FanTian = (id: any) => {
    router.push({
        path: "/Up",
        query: { id: id }
    })
}

</script>

<style scoped></style>